package com.zbcz.crm.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.zbcz.crm.CrmApplication
import com.zbcz.crm.bean.BaseBean
import com.zbcz.crm.bean.FollowPersonVo
import com.zbcz.crm.bean.VisitData
import com.zbcz.crm.network.CrmApi
import com.zbcz.crm.network.RetrofitManager
import com.zbcz.crm.sp.SharedPreferencesManager
import com.zbcz.crm.util.IS_TEST_MODE
import com.zbcz.crm.util.ToastUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class VisitorAddViewModel : ViewModel() {

    private val mApi = RetrofitManager.getRetrofit().create(CrmApi::class.java)

    val mFollowersList = MutableLiveData<List<FollowPersonVo>>()

    val mResponse = MutableLiveData<Boolean>()

    init {
        mFollowersList.postValue(emptyList())
    }

    fun addFollowers() {
        val currentList = mFollowersList.value.orEmpty().toMutableList()
        currentList.add(FollowPersonVo())
        mFollowersList.value = currentList
    }

    fun addTestFollowers() {
        val currentList = mFollowersList.value.orEmpty().toMutableList()
        currentList.add(generateTestFollowPersonVo())
        mFollowersList.value = currentList
    }

    private fun generateTestFollowPersonVo(): FollowPersonVo {
        return FollowPersonVo(
            followEmployer = if (IS_TEST_MODE) "测试单位" else "",
            followIdNo = if (IS_TEST_MODE) "110101199001012345" else "",
            followMobileNo = if (IS_TEST_MODE) "13800138000" else "",
            followName = if (IS_TEST_MODE) "张三" else ""
        )
    }


    fun removeFollower(position: Int) {
        val currentList = mFollowersList.value.orEmpty().toMutableList()
        if (position in 0 until currentList.size) {
            currentList.removeAt(position)
            mFollowersList.value = currentList
        }
    }

    fun removeFollower(vo: FollowPersonVo) {
        val currentList = mFollowersList.value.orEmpty().toMutableList()
        currentList.remove(vo)
        mFollowersList.value = currentList
    }

    fun submitOrder(data: VisitData) {
        viewModelScope.launch(Dispatchers.IO) {
            val result = submitOrderSuspend(data)
            withContext(Dispatchers.Main) {
                if (result?.success == true) {
                    mResponse.postValue(true)
                    ToastUtils.show("添加成功")
                } else {
                    result?.message?.let { ToastUtils.show(it) }
                    mResponse.postValue(false)
                }
            }
        }
    }


    private suspend fun submitOrderSuspend(data: VisitData): BaseBean<Object>? {
        try {
            return mApi.subMitOrder(
                data, token = SharedPreferencesManager.getToken(
                    CrmApplication.mApplication
                )
            )
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return null
    }


    override fun onCleared() {
        super.onCleared()
    }
}